/* SSD1306 Example

   See README.md file to get detailed usage of this example.

   This example code is in the Public Domain (or CC0 licensed, at your option.)

   Unless required by applicable law or agreed to in writing, this
   software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
   CONDITIONS OF ANY KIND, either express or implied.
*/
#include <stdio.h>
#include "esp_log.h"
#include "driver/i2c.h"
#include "driver/gpio.h"
#include "sdkconfig.h"
#include "OLEDDisplay.h"

static const char *TAG = "oled-example";

#define I2C_MASTER_SCL_IO   18          /*!< gpio number for I2C master clock */
#define I2C_MASTER_SDA_IO   17          /*!< gpio number for I2C master data  */
#define I2C_MASTER_NUM      1           /*!< I2C port number for master dev */

#define I2C_MASTER_RST_IO   21          /* 非标准模块,单oled显示屏需要复位  */
#define BOARD_GPIO_IO       35
#define VEX_CTRL_IO         36
#define OLED_ADDR           0x3c        // OLED的IIC地址，逻辑分析仪读出的

SemaphoreHandle_t print_mux = NULL;

static void Vex_and_GPIO_ON(void)
{
    gpio_reset_pin(VEX_CTRL_IO);
    gpio_set_direction(VEX_CTRL_IO, GPIO_MODE_OUTPUT);
    gpio_set_level(VEX_CTRL_IO, 0);
    gpio_reset_pin(BOARD_GPIO_IO);
    gpio_set_direction(BOARD_GPIO_IO, GPIO_MODE_OUTPUT);
    gpio_set_level(BOARD_GPIO_IO, 1);
}

static void OLED_reset(void)
{
    gpio_reset_pin(I2C_MASTER_RST_IO);
    gpio_set_direction(I2C_MASTER_RST_IO, GPIO_MODE_OUTPUT);
    gpio_set_level(I2C_MASTER_RST_IO, 1);
    for(uint32_t i=0;i<0x65536;i++);
    gpio_set_level(I2C_MASTER_RST_IO, 0);
    for(uint32_t i=0;i<0x65536;i++);
    gpio_set_level(I2C_MASTER_RST_IO, 1);
    for(uint32_t i=0;i<0x65536;i++);
}

static void i2c_test_task(void *arg)
{
    Vex_and_GPIO_ON();
    OLED_reset();
    OLEDDisplay_t *oled = OLEDDisplay_init(I2C_MASTER_NUM,OLED_ADDR,I2C_MASTER_SDA_IO,I2C_MASTER_SCL_IO);

    OLEDDisplay_fillRect(oled,7,7,18,18);
    OLEDDisplay_drawRect(oled,6,32,20,20);
    OLEDDisplay_display(oled);
    vTaskDelay(500 / portTICK_PERIOD_MS);

    OLEDDisplay_setTextAlignment(oled,TEXT_ALIGN_CENTER);
    OLEDDisplay_setFont(oled,ArialMT_Plain_24);
    OLEDDisplay_drawString(oled,64, 00, "Test");
    OLEDDisplay_display(oled);
    vTaskDelay(500 / portTICK_PERIOD_MS);

    OLEDDisplay_setFont(oled,ArialMT_Plain_16);
    OLEDDisplay_drawString(oled,64, 25, "Testing");
    OLEDDisplay_display(oled);
    vTaskDelay(500 / portTICK_PERIOD_MS);

    OLEDDisplay_setFont(oled,ArialMT_Plain_10);
    OLEDDisplay_drawString(oled,64, 40, "This is a test");
    OLEDDisplay_display(oled);

    while(1) {
        OLEDDisplay_setColor(oled,INVERSE);
        OLEDDisplay_fillRect(oled,6,6,20,20);
        OLEDDisplay_fillRect(oled,6,32,20,20);
        OLEDDisplay_display(oled);
        vTaskDelay(250 / portTICK_PERIOD_MS);
    }

    vSemaphoreDelete(print_mux);
    vTaskDelete(NULL);
}

void app_main(void)
{
    print_mux = xSemaphoreCreateMutex();
        ESP_LOGI(TAG,"Running");
    xTaskCreate(i2c_test_task, "i2c_test_task_0", 1024 * 4, (void *)0, 10, NULL);
}
